﻿using System;
using System.Collections.Generic;
using StockTrader.DAL.IDAL;
using StockTrader.BusinessService.DataContract;
using StockTrader.FrontEnd.DataContract;
using StockTrader.BusinessService.DataContract.Structure;
using StockTrader.BusinessService.DataContract.Hastc;
using StockTrader.DAL.IDAL.LoanMonitor;
using StockTrader.BusinessService.DataContract.Mortgate;
using StockTrader.FrontEnd.OrderTracker.Business;

namespace Mortgage.DAL
{
    public class MortgageMornitorDAL
    {
        IHoseOrders dalOrders;
        IStsStocks dalHastc;
        static IMortgage dalMortgage;

        #region Intraday Orders

        public List<OrderNewData> getNewConditionedOrderData()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getNewConditionedOrderData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<OrderChangeData> getOrderChangeData()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getOrderChangeData();
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<OrderCancelData> getOrderCancelData()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getOrderCancelData();
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrders.Close();
            }
        }


        public List<ConfirmOrderChangeData> getConfirmOrderChangeData()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getConfirmOrderChangeData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<ConfirmCrossingDealData> getConfirmCrossingDealData()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getConfirmCrossingDealData();
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<ConfirmDealData> getConfirmDealData()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getConfirmDealData();
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<ConfirmOrderCancelData> getConfirmOrderCancelData()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getConfirmOrderCancelData();
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<ConfirmOrderNewData> getConfirmOrderNewData()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getConfirmOrderNewData();
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrders.Close();
            }
        }


        public List<OFPTDealData> getOFPTDealData()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getOFPTDealData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<TFPTDealData> getTFPTDealData()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getTFPTDealData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<OrderPtCancelRequestData> getOrderPtCancelRequest()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getOrderPtCancelRequest();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<ConfirmPtDealData> getConfirmPtDealData()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getConfirmPtDealData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<ConfirmPtDealCancelData> getConfirmPtDealCancelData()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getConfirmPtDealCancelData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<PtACKData> getPtACKData()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getPtACKData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<PtDealReplyData> getPtDealReplyData()
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getPtDealReplyData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }


        public List<STSORDERS> getSTS_ORDERS()
        {
            dalHastc = StockTrader.DAL.Factory.StsStocksFactory.Create(Parameters.DBType);
            try
            {
                dalHastc.Open(Parameters.CNN);
                return dalHastc.getSTS_ORDERS();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalHastc.Close();
            }
        }
        public List<STSTRADING_RESULT> getSTS_TRADING_RESULT()
        {
            dalHastc = StockTrader.DAL.Factory.StsStocksFactory.Create(Parameters.DBType);
            try
            {
                dalHastc.Open(Parameters.CNN);
                return dalHastc.getSTS_TRADING_RESULT();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalHastc.Close();
            }
        }
        public List<SecuritiesInfo> getSTOCKSINFO()
        {
            dalHastc = StockTrader.DAL.Factory.StsStocksFactory.Create(Parameters.DBType);
            try
            {
                dalHastc.Open(Parameters.CNN);
                return dalHastc.getSTOCKSINFO();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalHastc.Close();
            }
        }
        public List<STSTOP3_PRICE_A> getSTS_TOP3_PRICE_A()
        {
            dalHastc = StockTrader.DAL.Factory.StsStocksFactory.Create(Parameters.DBType);
            try
            {
                dalHastc.Open(Parameters.CNN);
                return dalHastc.getSTS_TOP3_PRICE_A();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalHastc.Close();
            }
        }
        public List<STSTOP3_PRICE_B> getSTS_TOP3_PRICE_B()
        {
            dalHastc = StockTrader.DAL.Factory.StsStocksFactory.Create(Parameters.DBType);
            try
            {
                dalHastc.Open(Parameters.CNN);
                return dalHastc.getSTS_TOP3_PRICE_B();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalHastc.Close();
            }
        }
        public List<STSMARKETINFO> getSTS_MARKETINFO()
        {
            dalHastc = StockTrader.DAL.Factory.StsStocksFactory.Create(Parameters.DBType);
            try
            {
                dalHastc.Open(Parameters.CNN);
                return dalHastc.getSTS_MARKETINFO();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalHastc.Close();
            }
        }

        #endregion

        #region History Orders

        public List<OrderNewData> getBackupNewConditionedOrderData(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupNewConditionedOrderData(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<OrderChangeData> getBackupOrderChangeData(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupOrderChangeData(from, to);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<OrderCancelData> getBackupOrderCancelData(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupOrderCancelData(from, to);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrders.Close();
            }
        }


        public List<ConfirmOrderChangeData> getBackupConfirmOrderChangeData(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupConfirmOrderChangeData(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<ConfirmCrossingDealData> getBackupConfirmCrossingDealData(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupConfirmCrossingDealData(from, to);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<ConfirmDealData> getBackupConfirmDealData(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupConfirmDealData(from, to);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<ConfirmOrderCancelData> getBackupConfirmOrderCancelData(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupConfirmOrderCancelData(from, to);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<ConfirmOrderNewData> getBackupConfirmOrderNewData(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupConfirmOrderNewData(from, to);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrders.Close();
            }
        }


        public List<OFPTDealData> getBackupOFPTDealData(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupOFPTDealData(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<TFPTDealData> getBackupTFPTDealData(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupTFPTDealData(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<OrderPtCancelRequestData> getBackupOrderPtCancelRequest(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupOrderPtCancelRequest(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<ConfirmPtDealData> getBackupConfirmPtDealData(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupConfirmPtDealData(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<ConfirmPtDealCancelData> getBackupConfirmPtDealCancelData(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupConfirmPtDealCancelData(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<PtACKData> getBackupPtACKData(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupPtACKData(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }
        public List<PtDealReplyData> getBackupPtDealReplyData(DateTime from, DateTime to)
        {
            dalOrders = StockTrader.DAL.Factory.HoseOrdersFactory.Create(Parameters.DBType);
            try
            {
                dalOrders.Open(Parameters.CNN);
                return dalOrders.getBackupPtDealReplyData(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalOrders.Close();
            }
        }


        public List<STSORDERS> getSTS_ORDERSBackup(DateTime from, DateTime to)
        {
            dalHastc = StockTrader.DAL.Factory.StsStocksFactory.Create(Parameters.DBType);
            try
            {
                dalHastc.Open(Parameters.CNN);
                return dalHastc.getSTS_ORDERSBackup(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalHastc.Close();
            }
        }
        public List<STSTRADING_RESULT> getSTS_TRADING_RESULTBackup(DateTime from, DateTime to)
        {
            dalHastc = StockTrader.DAL.Factory.StsStocksFactory.Create(Parameters.DBType);
            try
            {
                dalHastc.Open(Parameters.CNN);
                return dalHastc.getSTS_TRADING_RESULTBackup(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalHastc.Close();
            }
        }
        public List<SecuritiesInfo> getSTOCKSINFOBackup(DateTime from, DateTime to)
        {
            dalHastc = StockTrader.DAL.Factory.StsStocksFactory.Create(Parameters.DBType);
            try
            {
                dalHastc.Open(Parameters.CNN);
                return dalHastc.getSTOCKSINFOBackup(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalHastc.Close();
            }
        }
        public List<STSTOP3_PRICE_A> getSTS_TOP3_PRICE_ABackup(DateTime from, DateTime to)
        {
            dalHastc = StockTrader.DAL.Factory.StsStocksFactory.Create(Parameters.DBType);
            try
            {
                dalHastc.Open(Parameters.CNN);
                return dalHastc.getSTS_TOP3_PRICE_ABackup(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalHastc.Close();
            }
        }
        public List<STSTOP3_PRICE_B> getSTS_TOP3_PRICE_BBackup(DateTime from, DateTime to)
        {
            dalHastc = StockTrader.DAL.Factory.StsStocksFactory.Create(Parameters.DBType);
            try
            {
                dalHastc.Open(Parameters.CNN);
                return dalHastc.getSTS_TOP3_PRICE_BBackup(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalHastc.Close();
            }
        }
        public List<STSMARKETINFO> getSTS_MARKETINFOBackup(DateTime from, DateTime to)
        {
            dalHastc = StockTrader.DAL.Factory.StsStocksFactory.Create(Parameters.DBType);
            try
            {
                dalHastc.Open(Parameters.CNN);
                return dalHastc.getSTS_MARKETINFOBackup(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalHastc.Close();
            }
        }
        #endregion

        #region Mortgage monitor
        public void ImportLoadRate(LoanRateData item)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                dalMortgage.InportLoadRate(item);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public decimal getLoanRate(DateTime date, string symbol)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getLoanRate(date, symbol);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public void DeleteLoanRate(DateTime date)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                dalMortgage.DeleteLoanRate(date);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }

        public void InsertMortgageTrans(string accountno, string contractno,
                                    DateTime date, string code, int type, decimal credit, decimal debit)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                dalMortgage.InsertMortgageTrans(accountno, contractno, date, code, type, credit, debit);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public void DeleteMortgageTransByContractNo(string contractno)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                dalMortgage.DeleteMortgageTransByContractNo(contractno);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }

        public List<MortgageTrans> getMortgageTransByCode(string contractno)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getMortgageTransByCode(contractno);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }

        public List<MortgageTrans> searchMortgageTrans(string code, string accountno, string contractno,
                                                DateTime from, DateTime to, int type,
                                                decimal lowDebit, decimal highDebit,
                                                decimal lowCredit, decimal highCredit)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.searchMortgageTrans(code, accountno, contractno, from, to, type,
                                                 lowDebit, highDebit, lowCredit, highCredit);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }


        public decimal getBlockingQttyByCode(string AccountNo, string code)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getBlockingQttyByCode(AccountNo, code);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }

        public decimal getBlockingQttyByCodeAndContract(string AccountNo, string code, string contractno)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getBlockingQttyByCodeAndContract(AccountNo, code, contractno);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }


        public string InsertCustomer(string Name, string Gender, string AccountNo, string IdcardNo, DateTime IdcardDate, string IdcardIssue, string PhoneMobile, string PhoneFix, string Email, string AddressContact, string AddressResident)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                dalMortgage.InsertCustomer(Name, Gender, AccountNo, IdcardNo, IdcardDate, IdcardIssue, PhoneMobile, PhoneFix, Email, AddressContact, AddressResident);

                return IdcardNo;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public string getCustomerIDByAccountNo(string AccountNo)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getCustomerByAccountNo(AccountNo);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public AccountData getCustomerByID(string CustomerID)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getCustomerByID(CustomerID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public void DeleteCustomerById(string CustomerID)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                dalMortgage.DeleteCustomerById(CustomerID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public List<AccountData> getCustomers()
        {            
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getCustomers();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }


        public int InsertFee(DateTime from, DateTime to, string Name, int type, decimal value, string customerid, string contractno, string accountno)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.InsertFee(from, to, Name, type, value, customerid, contractno, accountno);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public FeeData getFeeByCode(string ID)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getFeeByCode(ID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public FeeData getFeeByCodeAndDate(string Code, DateTime date)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getFeeByCodeAndDate(Code, date);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public FeeData getFeeByType(int type, string customerid, string contractno, string accountno)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getFee(type, customerid, contractno, accountno);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }

        public string InsertContract(string contractno, string customerid, DateTime start, DateTime end, decimal fee, decimal penaltyfee, decimal processfee, int contracttype, string status, decimal contractvalue, string AccountNo, string original)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                dalMortgage.InsertContract(contractno, customerid, start, end, fee, penaltyfee, processfee, contracttype, status, contractvalue, AccountNo, original);

                return contractno;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public ContractData getContractByNo(string contractno)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getContractByNo(contractno);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public List<ContractData> getcontractByCustomerId(string customerid)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getcontractByCustomerId(customerid);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public List<ContractData> getContractByDate(DateTime from, DateTime to)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getContractByStartDate(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public List<ContractData> getContractByValue(decimal from, decimal to)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getContractByValue(from, to);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public List<ContractData> searchContract(string contractno, string customerid,
                                            DateTime from, DateTime to, decimal lowvalue, 
                                            decimal highvalue, int contracttype, 
                                            string status, string AccountNo, string originalContract)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.searchContract(contractno, customerid, from, to, lowvalue,
                                            highvalue, contracttype, status, AccountNo, originalContract);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }

        public void InsertCustomerAccount(string customerId, string accounts)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                dalMortgage.InsertCustomerAccount(customerId, accounts);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public List<string> getCustomerAccounts(string customerId)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.getCustomerAccounts(customerId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public void deleteCustomerAccount(string customerId)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                dalMortgage.deleteCustomerAccount(customerId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }

        public decimal GetAccumulateLoanByAccountNo(string AccountNo)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                return dalMortgage.GetAccumulateLoanByAccountNo(AccountNo);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        public void UpdateAccumulateLoan(string AccountNo, decimal AccuValue, DateTime LastUpdate)
        {
            dalMortgage = StockTrader.DAL.Factory.MortgageFactory.Create(Parameters.DBType);
            try
            {
                dalMortgage.Open(Parameters.CNN);
                dalMortgage.UpdateAccumulateLoan(AccountNo, AccuValue, LastUpdate);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalMortgage.Close();
            }
        }
        #endregion
    }
}
